import { getDictionaryList } from '@/api/system';
import { useDictionaryStore } from './modules/dictionary';

export default function queryDictionaryList(data: AjaxResponse['dictionaryList'] = []) {
    // 缓存字典
    const useDictionary = useDictionaryStore();
    useDictionary.dictionary.length = 0;

    // 如果传入了数据
    if (data.length) {
        data.forEach((item) => {
            useDictionary.dictionary.push({
                word: item.word,
                definition: item.definition,
                width: item.width,
                type: item.type,
                roles: item.roles,
            });
        });
    } else {
        getDictionaryList().then((res) => {
            if (!(res.data && res.data.length)) {
                ElMessage.info('没有可用的字典列表');
                return;
            }

            // 加工成表单响应式数据
            res.data.forEach((item) => {
                useDictionary.dictionary.push({
                    word: item.word,
                    definition: item.definition,
                    width: item.width,
                    type: item.type,
                    roles: item.roles,
                });
            });
        });
    }
}
